package com.fanhang.samples.retrofit;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public abstract class ApiAdapter {
	
	public abstract <T> T create(Class<T> clazz);
	
	protected <T> T create(ClassLoader loader, Class<?>[] clazzs, InvocationHandler handler) {
		return (T) Proxy.newProxyInstance(loader, clazzs, handler);
	}
	
	protected class MethodHandler implements InvocationHandler {
		private String url;
		private String appKey;
		private String accessToken;
		private String sign;
		private String format = "";
		private String version = "";
		
		public MethodHandler(String url, String appKey, String accessToken, String sign, String format, String version) {
			this.url = url;
			this.appKey = appKey;
			this.accessToken = accessToken;
			this.format = format;
			this.version = version;
		}
		
		@Override
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			if (method.getDeclaringClass() == Object.class) {
				return method.invoke(this, args);
			}
			System.out.println(method.getName());
			return method.getReturnType();
		}
		
	}
}
